<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use think\Validate;
class CateController extends AdminBaseController
{
    public function index()
    {
        $cateModel = db::name('good_cate');
        $where = array();
        $params = $this->param();
        if(isset($params['name'])){
            $where[] = ['name','like','%'.$params['name'].'%'];
        }
        if(isset($params['status']) && $params['status']){
            $where[] = ['status','eq',$params['status']];
            $this->assign('status',$params['status']);
        }
        $cate = $cateModel->where($where)->order('path')->paginate(10,false,
            ['query'=>$params])->each(function($item, $key){
            if($item['pid'] == 0){
                $item['fname'] = '顶级分类';
            }else{
                $item['fname'] = db::name('good_cate')->where('id',$item['pid'])->value('name');
                $item['name'] = '++'.$item['name'];
            }
            return $item;
        });
        $this->assign('cate',$cate);
        $cate->appends($params);  //分页条件
        $this->assign('page', $cate->render());
        return $this->fetch();
    }

    public function add()
    {
        if($_POST){
            $validate = new Validate([
                'name'  => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            $_POST['addtime'] = date('Y-m-d H:i:s',time());
            $id = db::name('good_cate')->insertGetId($_POST);
            if($id){
                if($_POST['pid'] == 0){
                    $path = '0,'.$id;
                }else{
                    $path = db::name('good_cate')->where('id',$_POST['pid'])->value('path');
                    $path = $path.','.$id;
                }
                db::name('good_cate')->update(array('id'=>$id,'path'=>$path));
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
        //
        $cate = db::name('good_cate')->where('pid',0)->select();
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    public function edit()
    {
        $id = $this->param('id');
        $find = db::name('good_cate')->find($id);
        if($_POST){
            $validate = new Validate([
                'name'  => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            if($_POST['pid'] !== $find['pid']){
                $path = db::name('good_cate')->where('id',$_POST['pid'])->value('path');
                $_POST['path'] = $path.','.$find['id'];
            }
            $res = db::name('good_cate')->update($_POST);
            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }

        }else{
            $cate = db::name('good_cate')->where('pid',0)->select();
            $this->assign('cate',$cate);
            $this->assign('info',$find);
            return $this->fetch();
        }
    }
    public function update()
    {
        $param = $this->param();
        $res = db::name('good_cate')->update($param);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

}
